package com.doradance.app.mine;


import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.home.ClassEvent;
import com.doradance.app.home.CoursesDetailActivity;
import com.doradance.app.mine.adapter.MineCollectAdapter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.CollectDeleteEntity;
import com.doradance.entity.DeleteCollectEntity;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.IndexEntity;
import com.doradance.entity.SaveIdEntity;
import com.doradance.entity.SelectCallBack;
import com.doradance.entity.StartVideoEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineCollectFragment extends SwipeListFragment  {
    private int mPage = 0;
    private static final int MYLIVE_MODE_CHECK = 0;
    private static final int MYLIVE_MODE_EDIT = 1;
    private int mEditMode = MYLIVE_MODE_CHECK;
    private ArrayList<SaveIdEntity> idList;

    MineCollectAdapter mineCollectAdapter;
    public MineCollectFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
//        startLoading();
//        clearDecorations();
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        getData().clear();
        mPage = 1;
        startLoading();
        clearDecorations();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CollectDeleteEntity refreshEvent){
        if(refreshEvent.isShow==true){
            //需要编辑
            mEditMode =MYLIVE_MODE_EDIT;
        }else{
            //不需要编辑
            mEditMode = MYLIVE_MODE_CHECK;
        }
        mineCollectAdapter.setEditMode(mEditMode);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        FreeItemEntity o = (FreeItemEntity) getData().get(position);
        CoursesDetailActivity.start(getActivity(),o.getId());
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemLongClick(arg0, arg1, position, itemId);
        FreeItemEntity o = (FreeItemEntity) getData().get(position);
        deleteCollect(o.getId());
        return false;
    }

    private void deleteCollect(final int id) {
        LayoutInflater factory = LayoutInflater.from(getContext());
        View inflate = factory.inflate(R.layout.no_member_dialog, null);
        TextView tvDialogTitle = (TextView) inflate.findViewById(R.id.tv_dialog_title);
        TextView tvBecomeVip = (TextView) inflate.findViewById(R.id.tv_become_vip);
        TextView tvNoVip = (TextView) inflate.findViewById(R.id.tv_no_cancel);
        tvDialogTitle.setText("是否取消收藏该视频");
        tvBecomeVip.setText("确定");
        tvNoVip.setText("取消");
        final AlertDialog isMemberDialog = new AlertDialog.Builder(getContext())
                .setView(inflate).create();
        inflate.findViewById(R.id.tv_no_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isMemberDialog.cancel();
            }
        });
        inflate.findViewById(R.id.tv_become_vip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyHttpRequest request = new MyHttpRequest(getContext());
                String favoriteUrl = String.format(UrlCenter.FAVORITE_CANCEL, id);
                request.delete(favoriteUrl, null, new DataRequestListener<String>(String.class) {
                    @Override
                    public void onSuccess(String o) {
                        super.onSuccess(o);
                        isMemberDialog.cancel();
                        getData().clear();
                        mineCollectAdapter.notifyDataSetChanged();
                        mPage = 1;
                        startLoading();
                    }
                    @Override
                    public void onFailure(Throwable t, int errorNo, String strMsg) {
                        super.onFailure(t, errorNo, strMsg);
                        EasyToast.showToast(getContext(),strMsg);
                        isMemberDialog.cancel();
                    }
                });
            }
        });
        //设置点击Dialog外部任意区域关闭Dialog
        isMemberDialog.setCanceledOnTouchOutside(true);
        isMemberDialog.show();

    }


    @Override
    public View createEmptyView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.empty_collect,null);
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 1;
        startLoading();
    }
    @Override
    public RecyclerViewBaseAdapter createAdapter() {

        mineCollectAdapter = new MineCollectAdapter(getActivity());
        return mineCollectAdapter;
    }
    private ListDataRequestListener<FreeItemEntity> mResponceListener = new ListDataRequestListener<>(this, FreeItemEntity.class);
    @Override
    public void onStartLoading() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
            MyHttpRequest request = new MyHttpRequest(getContext());
            UrlParams urlParams = new UrlParams();
            request.get(UrlCenter.COURSES_FAVORITE_LIST, urlParams, mResponceListener);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(SelectCallBack refreshEvent){
//        idList = refreshEvent.idList;
//        int position = refreshEvent.position;
//        if(idList.get(position).getSelect()==true){
//            idList.get(position).setSelect(false);
//            mineCollectAdapter.getPosition(position,false);
//        }else{
//            idList.get(position).setSelect(true);
//            mineCollectAdapter.getPosition(position,true);
//        }
//        mineCollectAdapter.notifyDataSetChanged();
////        mineCollectAdapter.notifyItemChanged(position);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(DeleteCollectEntity refreshEvent){
        ArrayList<Integer> id= new ArrayList<>();
        for(int i=0;i<idList.size();i++){
            if(idList.get(i).getSelect()==true){
                id.add(idList.get(i).getId());
            }
        }
        if(id!=null){
        }else{
            EasyToast.showToast(getContext(),"请选择需要删除的视频");
        }
    }
}
